package net.bac.sbe.web.report;

import java.io.InputStream;
import java.util.Map;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.JRXlsExporter;
import net.sf.jasperreports.engine.export.JRXlsExporterParameter;
import tests.helloworld.JasperHelloWorld;

public class ReportManager {
	public static void generatePDFReport(String path, String reportName,
			Map<String, Object> parameters) {
		generatePDFReport(path, reportName, parameters, new JREmptyDataSource());
	}

	public static void generatePDFReport(String path, String reportName,
			Map<String, Object> parameters, JRDataSource dataSource) {
		JasperPrint jasperPrint;
		try {
			InputStream inputStream = JasperHelloWorld.class
					.getResourceAsStream(path);
			jasperPrint = JasperFillManager.fillReport(inputStream, parameters,
					dataSource);
			JasperExportManager.exportReportToPdfFile(jasperPrint, reportName);

		} catch (JRException e) {
			e.printStackTrace();
		}
	}

	public static void generateXLSReport(String path, String reportName,
			Map<String, Object> parameters) {
		JasperPrint jasperPrint;
		try {
			InputStream inputStream = JasperHelloWorld.class
					.getResourceAsStream(path);
			jasperPrint = JasperFillManager.fillReport(inputStream, parameters,
					new JREmptyDataSource());

			JRXlsExporter exporter = new JRXlsExporter();
			exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
			exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, reportName);
			exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
			exporter.exportReport();
		} catch (JRException e) {
			e.printStackTrace();
		}
	}
}
